#include "core/operondb.h"

static int _openTxn(struct ${db.typedef}* ptr,	DB_TXN *txnid,u_int32_t  flags)
	{
	int rez;
	assert(ptr!=NULL);
	if(ptr->isOpen(ptr))
		{
		return -1;
		}
	rez=(ptr->db->open(
		ptr->db,
		txnid,
		ptr->filename(ptr),
		ptr->databasename(ptr),
		ptr->method(ptr),
		flags,
		ptr->mode(ptr)
		));
	if(rez!=0)
		{
		return rez;
		}
	ptr->operon->openDB(ptr);
	return 0;
	}

static int _open(struct ${db.typedef}* ptr)
	{
	return _openTxn(ptr,NULL,0);
	}

static int _close(struct ${db.typedef}* ptr)
	{
	int rez;
	assert(ptr!=NULL);
	if(!ptr->isOpen(ptr))
		{
		return 0;
		}
	rez = ptr->db->close(ptr->db);
	ptr->db=NULL;
	ptr->operon->closeDB(ptr);
	return rez;
	}

int initOperonDBP(OperonDBPtr ptr,OperonPtr operon)
	{
	assert(ptr!=NULL);
	memset(ptr,0,sizeof(OperonDB));
	ptr->operon=operon;
	ptr->isOpen=isOpen;
	ptr->open=_open;
	ptr->openTxn=_openTxn;
	ptr->close->_close;
	return 0;
	}
